package com.sinosoft.web.common.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by Administrator on 2016/2/19.
 */
public class MyFastJsonHttpMessageConverter extends FastJsonHttpMessageConverter {

    public MyFastJsonHttpMessageConverter() {
        super();
    }

    protected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException,
            HttpMessageNotReadableException {
        return super.readInternal(clazz, inputMessage);
    }


    protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException,
            HttpMessageNotWritableException {

        OutputStream out = outputMessage.getBody();
        String result = (obj instanceof String) ? (String) obj : JSON.toJSONString(obj, getFeatures());
        byte[] bytes = result.getBytes(getCharset());
        out.write(bytes);
    }
}
